package com.xsq.newmall01.Controller;

import com.xsq.newmall01.Dao.Good;
import com.xsq.newmall01.Dao.MerchantUser;
import com.xsq.newmall01.Dao.User;
import com.xsq.newmall01.Service.MerchantUserService.MGoodService;
import com.xsq.newmall01.Service.UserService.UGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodController {

    // 商家商品服务（上下架）
    @Autowired
    @Qualifier("merchantgoodservice") // 指定Bean名称为实现类定义的"merchantgoodservice"
    private MGoodService merchantMGoodService;

    // 用户商品服务（查询）
    @Autowired
    @Qualifier("usergoodservice") // 指定Bean名称为实现类定义的"usergoodservice"
    private UGoodService userUGoodService;

    /**
     * 商家上架商品
     */
    @PostMapping("/add")
    public Map<String, Object> addGood(@RequestBody Good good, @RequestParam int merchantId) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 验证价格是否为正数（分单位）
            if (good.getPrice() <= 0) {
                throw new IllegalArgumentException("价格必须大于0");
            }
            merchantMGoodService.addGood(good, merchantId);
            result.put("success", true);
            result.put("message", "商品上架成功");
        } catch (IllegalArgumentException e) {
            result.put("success", false);
            result.put("message", "商品上架失败：" + e.getMessage());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "商品上架失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 商家下架商品
     */
    @DeleteMapping("/remove")
    public Map<String, Object> removeGood(@RequestParam int goodId, @RequestParam int merchantId) {
        Map<String, Object> result = new HashMap<>();
        try {
            merchantMGoodService.removeGood(goodId, merchantId);
            result.put("success", true);
            result.put("message", "商品下架成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "商品下架失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 用户查询所有商品
     */
    @GetMapping("/all")
    public Map<String, Object> getAllGoods() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Good> goods = userUGoodService.getAllGoods();
            result.put("success", true);
            result.put("data", goods);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/all-with-merchant")
    public Map<String, Object> getAllGoodsWithMerchantInfo() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Good> goods = userUGoodService.getAllGoodsWithMerchantInfo();
            result.put("success", true);
            result.put("data", goods);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }


    /**
     * 用户查询自己拥有的商品
     */
    @PostMapping("/owned")
    public Map<String, Object> getOwnedGoods(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Good> goods = userUGoodService.getOwnedGoods(user);
            result.put("success", true);
            result.put("data", goods);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @PostMapping("/merchant/owned")
    public Map<String, Object> getMerchantOwnedGoods(@RequestBody MerchantUser merchantUser) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 调用商家服务查询其上架的商品（关联 userkind=2）
            List<Good> goods = merchantMGoodService.getMerchantOwnedGoods(merchantUser);
            result.put("success", true);
            result.put("data", goods);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
}